//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/ienum/subject/chapter_icon_enum.dart';
import 'package:hktk_app/model/course/video_info_model.dart';
import 'package:hktk_app/pages/course/course_detail_page.dart';
import 'package:hktk_app/util/event_bus_util.dart';

import 'package:hktk_app/widget/subject/chapter_show_widget.dart';

/// 课程章节叶子节点显示
class CourseLeafWidget extends StatelessWidget {
  /// 视频相关信息
  final VideoInfoModel infoModel;

  /// 栏目id
  final String columnId;

  /// 免费标志，true：免费/购买，false：未购买
  final bool free;

  /// 章节id
  final String chapterId;

  /// 章节名称
  final String chapterName;

  /// 是否是课程详情进入，默认为false，true：是，false：否
  final bool courseDetail;

  const CourseLeafWidget({
    super.key,
    required this.infoModel,
    required this.columnId,
    required this.free,
    required this.chapterId,
    required this.chapterName,
    required this.courseDetail,
  });

  @override
  Widget build(BuildContext context) {

    // return Text("222222222");
    return ChapterShowWidget(
      title: infoModel.name ?? "",
      iconEnum: free ? ChapterIconEnum.course : ChapterIconEnum.lock,
      directShow: false,
      onTapFun: _onTapFun,
      total: infoModel.duration ?? 1,
      num: infoModel.watchDuration ?? 0,
      showQuestion: false,
      isFinished: infoModel.fs ?? false,

    );
  }

  /// 点击方法
  void _onTapFun(BuildContext context) {
    if (free) {
      // todo 切换时，已观看结束的不改变状态
      // 播放完成后保存一次记录，同时改变字段fs的值
      // 已购买
      if (courseDetail) {
        // 从课程详情进入，点击切换对应视频
        // 获取当前观看时长
        // 如果是自己，则不切换
        // if(true){
        //
        // }
        eventBus.fire(EventCourseDetailVideoSwitch(
          infoModel: infoModel,
          chapterId: chapterId,
          chapterName: chapterName,
        ));
      } else {
        // 不是从课程详情进入，点击进入课程详情
        Navigator.pushNamed(context,'/course-detail',arguments: {
              "infoModel": infoModel,
              "chapterId": chapterId,
              "chapterName": chapterName,
              "columnId": columnId,
              "free": free,
        });
      }
    }
  }
}
